<template>
  <div>
    <my-loading v-show="isShow"></my-loading>
    <van-dialog v-model="show" title="提示" show-cancel-button @confirm='toCurrentCity(currentCityId)' :currentCityId='currentCityId'>
      <p class="location">当前定位: <van-icon name="location" /> {{currentCity}} <br>是否切换?</p>
    </van-dialog>
    <div class="movie_body">
      <my-scroller :handleScroll="handleScroll" :handleTouchEnd="handleTouchEnd">
        <ul>
          <li v-for="(item) in movieList" :key="item.id">
            <div class="pic_show">
              <img :src="item.img | formatImg('128.180')" @tap="toDeatil(item.id)"  />
            </div>
            <div class="info_list">
              <h2 @tap='toDeatil(item.id)'>{{item.nm}}</h2>
              <img src="@/assets/maxs.png" v-if="item.version" />
              <p>
                观众评
                <span class="grade">{{item.sc}}</span>
              </p>
              <p>主演: {{item.star}}</p>
              <p>{{item.showInfo}}</p>
            </div>
            <div class="btn_mall">购票</div>
          </li>
        </ul>
      </my-scroller>
    </div>
  </div>
</template>


<script>
export default {
  name: 'playingNow',
  data() {
    return {
      movieList: [],
      message: '',
      isShow: false,
      checkId: -1,
      show: false,
      currentCity:'',
      currentCityId:-1
    }
  },
  methods: {
    async getHotMovies() {
      let res = await this.$axios.get(`/api/movieOnInfoList?cityId=${this.$store.state.city.id}`)
      if (res.msg !== 'ok') {
        console.log('获取电影信息失败:' + res.msg)
        return
      }
      this.movieList = res.data.movieList
      this.isShow = false
      this.checkId = this.$store.state.city.id
    },
    toDeatil(id) {
      this.$router.push(`/movie/detail/${id}`)
    },
    async handleScroll() {
      this.isShow = true
    },
    async handleTouchEnd() {
      let res = await this.$axios.get(`/api/movieOnInfoList?cityId=${this.$store.state.city.id}`)
      if (res.msg !== 'ok') {
        console.log('获取电影信息失败:' + res.msg)
        return
      }
      setTimeout(() => {
        this.movieList = res.data.movieList
        this.isShow = false
        this.checkId = this.$store.state.city.id
      }, 1000)
    },
    async toCurrentCity(id){
       let res = await this.$axios.get(`/api/movieOnInfoList?cityId=${id}`)
      if (res.msg !== 'ok') {
        console.log('获取电影信息失败:' + res.msg)
        return
      }
      this.movieList = res.data.movieList
      this.isShow = false
      this.$store.state.city.id = id
      this.$store.state.city.nm = this.currentCity
      this.checkId = this.$store.state.city.id
    },

  },
  async mounted() {
    let res = await this.$axios.get(`/api/getLocation`)
    console.log(res)
    if(res.msg!=='ok')return this.$toast('获取定位信息失败')
    this.currentCity = res.data.nm
    this.currentCityId = res.data.id
    if(res.data.id == this.$store.state.city.id)return;
    setTimeout(() => {
      this.show = true
    }, 2000)
  },
  activated() {
    if (this.checkId === this.$store.state.city.id) return
    this.getHotMovies()
  }
}
</script>

<style scoped>
.movie_body {
  flex: 1;
  overflow: auto;
  height: 520px;
}
.movie_body ul {
  margin: 0 12px;
  overflow: hidden;
}
.movie_body ul li {
  margin-top: 12px;
  display: flex;
  align-items: center;
  border-bottom: 1px #e6e6e6 solid;
  padding-bottom: 10px;
}
.movie_body .pic_show {
  width: 64px;
  height: 90px;
}
.movie_body .pic_show img {
  width: 100%;
}
.movie_body .info_list {
  margin-left: 10px;
  flex: 1;
  position: relative;
}
.movie_body .info_list h2 {
  font-size: 17px;
  line-height: 24px;
  width: 150px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.movie_body .info_list p {
  font-size: 13px;
  color: #666;
  line-height: 22px;
  width: 200px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.movie_body .info_list .grade {
  font-weight: 700;
  color: #faaf00;
  font-size: 15px;
}
.movie_body .info_list img {
  width: 50px;
  position: absolute;
  right: 10px;
  top: 5px;
}
.movie_body .btn_mall,
.movie_body .btn_pre {
  width: 47px;
  height: 27px;
  line-height: 28px;
  text-align: center;
  background-color: #f03d37;
  color: #fff;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
}
.movie_body .btn_pre {
  background-color: #3c9fe6;
}
.location {
  text-align: center;
  font-size: 18px;
  line-height: 24px;
}
</style>
